package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.UserShoppingCartMapper;
import com.sky.service.UserShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserShoppingCartServiceImpl implements UserShoppingCartService {

    @Autowired
    private UserShoppingCartMapper userShoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public void add2ShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> cartList = userShoppingCartMapper.selectShoppingCart(shoppingCart);
        //判断购物车表中是否有数据，有就更新数据库，没有就添加
        if (cartList != null && cartList.size() > 1 ) {
            shoppingCart = cartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            userShoppingCartMapper.updateByNumber(shoppingCart);
        } else {
            //判断是添加菜品还是套餐
            if (shoppingCartDTO.getDishId() != null ) {
                Dish dish = dishMapper.getById(shoppingCart.getId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            } else {
                //添加套餐
                Setmeal setmeal = setMealMapper.getByDishId(shoppingCart.getId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }

            shoppingCart.setId(1L);
            shoppingCart.setCreateTime(LocalDateTime.now());
            userShoppingCartMapper.insert();

        }
    }

    @Override
    public List<ShoppingCart> selectListShoppingCart() {
        return userShoppingCartMapper.selectListShoppingCart(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    @Override
    public void deleteShoppingCart() {
        userShoppingCartMapper.delete(BaseContext.getCurrentId());

    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = userShoppingCartMapper.selectShoppingCart(shoppingCart);

        if(list != null && list.size() > 0){
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            if(number == 1){

                userShoppingCartMapper.deleteById(shoppingCart.getId());
            }else {

                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                userShoppingCartMapper.updateByNumber(shoppingCart);
            }
        }
    }
}
